//
//  WaterFallHeaderReusableView.m
//  FNMenu
//
//  Created by 航汇聚科技 on 2018/9/5.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "WaterFallHeaderReusableView.h"
#import <SDCycleScrollView/SDCycleScrollView.h>
#import "NSDictionary+Safety.h"
#import "NSArray+Safety.h"
#import "LocationHandler.h"
#import "VerticalCenterButton.h"
@interface WaterFallHeaderReusableView ()<SDCycleScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIView *cycleView;
@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;
@property (weak, nonatomic) IBOutlet UILabel *firstLabel;
@property (weak, nonatomic) IBOutlet UIImageView *firstImgView;
@property (weak, nonatomic) IBOutlet UILabel *secondLabel;
@property (weak, nonatomic) IBOutlet UIImageView *secondImgView;
@property (weak, nonatomic) IBOutlet UILabel *thirdLabel;
@property (weak, nonatomic) IBOutlet UIImageView *thirdImgView;
@property (weak, nonatomic) IBOutlet UILabel *fourthLabel;
@property (weak, nonatomic) IBOutlet UIImageView *fourthImgView;

@property (weak, nonatomic) IBOutlet UILabel *locationLabel;
@end

@implementation WaterFallHeaderReusableView

#pragma mark - SDCycleScrollViewDelegate
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"点击了第%ld张",(long)index);
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectItemAtIndex:)]) {
        [self.delegate didSelectItemAtIndex:index];
    }
}
#pragma mark - notification
- (void)updateLocationInfo:(NSNotification *)notification {
    NSDictionary *info = notification.userInfo;
    if (self.delegate && [self.delegate respondsToSelector:@selector(locationName:)]) {
        NSString *locality =[info valueForKey:locationLocality];
        NSLog(@"%@",info);
        if ([locality containsString:@"市"]) {
            locality = [[locality componentsSeparatedByString:@"市"] firstObject];
        }
        if ([locality containsString:@"省"]) {
            locality = [[locality componentsSeparatedByString:@"省"] firstObject];
        }
        [self.delegate locationName:locality];
        self.locationLabel.text = locality;
    }
}
#pragma mark - event response
- (IBAction)locationAction:(UIButton *)sender {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLocationInfo:) name:NOTFICATION_LOCATIONALLINFO object:nil];
    
    LocationHandler *location = [LocationHandler defaultLocationHandler];
    if ([location isAuthLocation]) {
        [location startUpdatingLocation];
    }else {
        [location requestLocationAuthorization];
    }
}
#pragma mark - public
- (void)updateLocationIfAuth {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateLocationInfo:) name:NOTFICATION_LOCATIONALLINFO object:nil];
    LocationHandler *location = [LocationHandler defaultLocationHandler];
    if ([location isAuthLocation]) {
        [location startUpdatingLocation];
    }
}

#pragma mark - setter
- (void)setFoodKinds:(NSArray *)foodKinds {
    _foodKinds = foodKinds;
    [self.cycleView addSubview:self.cycleScrollView];
    self.cycleScrollView.delegate = self;
    
    self.firstLabel.text = [[foodKinds safeObjectAtIndex:0] safeObjectForKey:@"name"];
    self.secondLabel.text = [[foodKinds safeObjectAtIndex:1] safeObjectForKey:@"name"];
    self.thirdLabel.text = [[foodKinds safeObjectAtIndex:2] safeObjectForKey:@"name"];
    self.fourthLabel.text = [[foodKinds safeObjectAtIndex:3] safeObjectForKey:@"name"];
    
    self.fourthImgView.userInteractionEnabled = YES;
    self.fourthLabel.userInteractionEnabled = YES;
    
    self.thirdLabel.userInteractionEnabled = YES;
    self.thirdImgView.userInteractionEnabled = YES;
    
    self.secondLabel.userInteractionEnabled = YES;
    self.secondImgView.userInteractionEnabled = YES;
    
    self.firstLabel.userInteractionEnabled = YES;
    self.firstImgView.userInteractionEnabled = YES;
}
#pragma mark - getter
- (SDCycleScrollView *)cycleScrollView {
    if (!_cycleScrollView) {
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:self.cycleView.bounds imageNamesGroup:@[@"group_01", @"group_02", @"group_03"]];
        _cycleScrollView.delegate = self;
    }
    return _cycleScrollView;
}
#pragma mark - dealloc
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:NOTFICATION_LOCATIONALLINFO object:nil];
}
@end
